package com.bootdo.lvyou_specialty.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.util.StringUtils;
import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.BuildTree;
import com.bootdo.lvyou.domain.LvyouCity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.bootdo.common.controller.BaseController;
import com.bootdo.lvyou_specialty.domain.LvyouSpecialtyMenu;
import com.bootdo.lvyou_specialty.service.LvyouSpecialtyMenuService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author haojiangbo
 * @email 2100272728@qq.com
 * @date 2019-10-10 11:39:51
 */
 
@Controller
@RequestMapping("/lvyou_specialty/lvyouSpecialtyMenu")
public class LvyouSpecialtyMenuController extends BaseController{


	@Autowired
	private LvyouSpecialtyMenuService lvyouSpecialtyMenuService;
	
	@ModelAttribute
	public LvyouSpecialtyMenu get(@RequestParam(required=false) Long id) {
		if (id != null){
			return lvyouSpecialtyMenuService.get(id);
		}else{
			return new LvyouSpecialtyMenu();
		}
	}


	
	//insert into sys_menu(parent_id,name,url,perms,type)  values (149,'测试','/lvyou_specialty/lvyouSpecialtyMenu','lvyou_specialty:lvyouSpecialtyMenu:lvyouSpecialtyMenu',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (160,'修改','','lvyou_specialty:lvyouSpecialtyMenu:edit',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyMenu:lvyouSpecialtyMenu")	
	String LvyouSpecialtyMenu(LvyouSpecialtyMenu lvyouSpecialtyMenu,HttpServletRequest request,HttpServletResponse response){
	    return "lvyou_specialty/lvyouSpecialtyMenu/list";
	}
	
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyMenu:lvyouSpecialtyMenu")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<LvyouSpecialtyMenu> lvyouSpecialtyMenuList = lvyouSpecialtyMenuService.list(query);
		int total = lvyouSpecialtyMenuService.count(query);
		PageUtils pageUtils = new PageUtils(lvyouSpecialtyMenuList, total);
		return R.ok().put("rows", lvyouSpecialtyMenuList).put("total", total);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyMenu:edit")
	String add(LvyouSpecialtyMenu lvyouSpecialtyMenu,HttpServletRequest request,HttpServletResponse response){
	    return "lvyou_specialty/lvyouSpecialtyMenu/lvyouSpecialtyMenuedit";
	}

	@PostMapping("/getById")
	@ResponseBody
	R getById(LvyouSpecialtyMenu lvyouSpecialtyMenu) {
		return R.ok().put("i",lvyouSpecialtyMenu);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyMenu:edit")
	public R save( LvyouSpecialtyMenu lvyouSpecialtyMenu){

		if (lvyouSpecialtyMenu.getParentId() == -1) {
			lvyouSpecialtyMenu.setParentId(0L);
		}
		if(lvyouSpecialtyMenuService.save(lvyouSpecialtyMenu)>0){
			return R.ok();
		}
		return R.error();
	}
	
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyMenu:edit")
	public R remove( Long id){
		if(lvyouSpecialtyMenuService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyMenu:edit")
	public R remove(@RequestParam("ids[]") Long[] ids){
		lvyouSpecialtyMenuService.batchRemove(ids);
		return R.ok();
	}

	// 树形图
	@RequestMapping("/tree")
	@ResponseBody
	@RequiresPermissions("lvyou:lvyouCity:edit")
	public R tree(HttpServletRequest request){
		Integer parentId =  StringUtils.isEmpty(request.getParameter("parentId")) ? null:Integer.parseInt(request.getParameter("parentId"));
		List list = new ArrayList();

		if(null == parentId) {
			list.add(getTree());
			return R.ok().put("l",list ) ;
		}else {
			list.add(getTree(parentId));
			return R.ok().put("l", list);
		}

	}

	public Tree<LvyouCity> getTree() {
		List<Tree<LvyouCity>> trees = new ArrayList<Tree<LvyouCity>>();
		HashMap<String,Object> params  = new HashMap<String,Object>(16);
		/*params.put("userId", getUserId());*/
		List<LvyouSpecialtyMenu> sysDepts = lvyouSpecialtyMenuService.list(params);
		for (LvyouSpecialtyMenu sysDept : sysDepts) {
			Tree<LvyouCity> tree = new Tree<LvyouCity>();
			tree.setId(sysDept.getId().toString());
			tree.setParentId(sysDept.getParentId().toString());
			tree.setText(sysDept.getName());
			Map<String, Object> state = new HashMap<>(16);
			state.put("opened", true);
			tree.setState(state);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<LvyouCity> t = BuildTree.build(trees);
		return t;
	}

	/**
	 * 如果传入的id 和  树里的数据相等 就选中
	 * @param id
	 * @return
	 */
	public Tree<LvyouCity> getTree(Integer id) {

		// 根据roleId查询权限
		List<Tree<LvyouCity>> trees = new ArrayList<Tree<LvyouCity>>();
		List<LvyouSpecialtyMenu> menuDOs = lvyouSpecialtyMenuService.list(new HashMap<String, Object>(16));
		for (LvyouSpecialtyMenu sysMenuDO : menuDOs) {
			Tree<LvyouCity> tree = new Tree<LvyouCity>();
			tree.setId(sysMenuDO.getId().toString());
			tree.setParentId(sysMenuDO.getParentId().toString());
			tree.setText(sysMenuDO.getName());
			Map<String, Object> state = new HashMap<>(16);
			Long menuId = sysMenuDO.getId();
			if (menuId.longValue() == id) {
				state.put("selected", true);
			} else {
				state.put("selected", false);
			}
			tree.setState(state);
			trees.add(tree);

		}

		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<LvyouCity> t = BuildTree.build(trees);

		// 小bug
		if(id.equals(-1)) {
			Map<String, Object> state = new HashMap<>(16);
			state.put("selected", true);
			t.setState(state);
		}

		return t;
	}
	
}